<?php
/**
 * 
 * @author szm
 *
 */

class Dialogue extends CI_Controller {

	private function getVocabulariesByLessonId($lessonId){
        $this->load->model('Vocabulary_model');
        $this->load->model('Dialogue_model');
        $vocabularies=$this->Vocabulary_model->getAllItems($lessonId);
        $vocabularies=$this->indexArray($vocabularies,'sentence_id');
		return  $vocabularies;
	}
	/**
	 * one to more relation ship
	 * @param array $arrays Two-dimensional array
	 * @param string $key
	 * @return array;
	 */									
	private function indexArray($arrays,$index){
	    if(empty($arrays)){
	        return false;
	    }
	    $tempArr=$arrays;
	    $arrays=array();
	    foreach($tempArr as $key=>$arr){
    		$arrays[$arr[$index]][]=$arr;
	    }
	    return $arrays;
	}
	
	
    private function getDialogueSentence($lessonId){
        $this->load->model('Dialogue_model');
        $sentences=$this->Dialogue_model->getDialogues($lessonId);
		return $sentences;
    }
    /**
     * you can put it to help
     */
    private function changeArrays($arrs){
        $resultArr=array();
        $result=array();
        while($temp=array_pop($arrs)){
            if(is_array($temp)){
                $result=$this->changeArrays($temp);
            }else{
                $resultArr[]=$temp;
                
            }

        } 
       $tempArr= array_merge($result,$resultArr);
       return array_unique($tempArr);
    
    }
    
    
    public function index(){
        $this->load->helper('url');
        $lessonId= $_REQUEST['lesson_id']?$_REQUEST['lesson_id']:1;
       // $this->load->model('Dialogue_model');
        $this->load->helper('parsesentance');
        
        $sentences=$this->getDialogueSentence($lessonId);
		$vocabularies=$this->getVocabulariesByLessonId($lessonId);
		
		$pageMeta=array('sentences'=>$sentences,'vocabularies'=>$vocabularies);
        $this->load->view('dialogue/index',array('pageMeta'=>$pageMeta));
    }
    

    public function getDialogues(){
        $data=array();
        $this->load->model('Dialogue_model');
        $data=$this->Dialogue_model->test();
        $this->load->view('dialogues',$data);
    }
    public function listAllDialogues(){
        $data=array();
        $this->load->model('Dialogue_model');
        $this->Dialog_model->listAllDialogues();
        $this->load->view('list_all_dialogues');
    }
    
    public function getDialogue(){
        $data=array();
        $this->load->view('dialogue');
    }
    
    public function saveDialogue(){
        $data=array();
        redirect('/lesson/index/', 'refresh');

    }
    
    public function removeDialogue(){
        $data=array();
        redirect('/lesson/index/', 'refresh');
    }
}

        //$sentanceModel
        
        //$dialogueModel=$this->Dialogue_model;
        //$sampleSentenceIdArrs=$dialogueModel->getSampleSentanceIds($lessonId);
        //$this->changeArrays($sampleSentenceIdArrs);
        //    $data=array();
        //    $sampleSentenceId=0;
         //   $data[$sampleSentenceId]['source']="鍡紒濂戒箙涓嶈锛�";
         //   $vocabularies[$sampleSentenceId]=array('鍡�','濂戒箙涓嶈');
       // return parsesentance($data,$vocabularies);